var oAudio,images,srcs,preIndex,length;
$(document).ready(init());
function init(){
	srcs = new Array();
	images = new Array();
	$trs = $('table[class="track_list"]>tbody',window.opener.document).children();
	$('#musicList').html('');
	length = $trs.length;
	lis = $('div[class="quote_song_list"]>ul',window.opener.document).children();
	lis2 = $('#music_list',window.opener.document).children();
	if(length>0){
		for(i=0;i<$trs.length;i++){
			path = $('table[class="track_list"]>tbody>tr:eq('+i+')',window.opener.document).attr('path');
			name = $('table[class="track_list"]>tbody>tr:eq('+i+')>td:eq(0)>a',window.opener.document).text();
			images[i] = $('table[class="track_list"]>tbody>tr:eq('+i+')',window.opener.document).attr('source');
			srcs[i] = '/ShareMusicProject'+path;
			$('#musicList').append('<li onclick="select(this);" num="'+i+'" class="">'+name+'</li>');
		}
	}else if(lis.length>0){
		type = $('#playType',window.opener.document)[0].value;
		for(i=0;i<lis.length;i++){
			if((type == 1)||($('div[class="quote_song_list"]>ul>li:eq('+i+')>div>div>span>input',window.opener.document).attr('checked')=="checked")){
				path = $('div[class="quote_song_list"]>ul>li:eq('+i+')',window.opener.document).attr('data-source');
				name = $('div[class="quote_song_list"]>ul>li:eq('+i+')',window.opener.document).attr('data-name');
				images[i] = $('div[class="quote_song_list"]>ul>li:eq('+i+')',window.opener.document).attr('data-img');
				srcs[i] = '/ShareMusicProject'+path;
				$('#musicList').append('<li onclick="select(this);" num="'+i+'" class="">'+name+'</li>');
			}
		}
	}else if(lis2.length>0){
		for(i=0;i<lis2.length;i++){
			if($('#music_list>li:eq('+i+')>div>div>span>input',window.opener.document).attr('checked')=="checked"){
				path = $('#music_list>li:eq('+i+')',window.opener.document).attr('data_source');
				name = $('#music_list>li:eq('+i+')',window.opener.document).attr('data_name');
				images[i] = $('#music_list>li:eq('+i+')',window.opener.document).attr('data_img');
				srcs[i] = '/ShareMusicProject'+path;
				$('#musicList').append('<li onclick="select(this);" num="'+i+'" class="">'+name+'</li>');
			}
		}
	}
	
	
	oAudio = document.createElement('audio');
	oAudio.addEventListener("timeupdate", showtime, true);
	oAudio.src = srcs[0];
	$('#bg_img').attr('src',images[0]);
	preIndex = 0;
}

function showtime() {
	$('#timer').text(timeDeal(oAudio.currentTime) + "/" + timeDeal(oAudio.duration));
}

function play(){
		
	            // Check for audio element support.
        if (window.HTMLAudioElement) {
            try {
                //console.log(oAudio.src);
                // Tests the paused attribute and set state. 
				//oAudio = document.getElementById('player');
                if (oAudio.paused) {
                    oAudio.play();
					$('#playIco').removeClass('icoplay').addClass('icopause');
                    //document.getElementById('playIndicator').innerHTML = '立刻暂停';
                }
                else {
                    oAudio.pause();
					$('#playIco').removeClass('icopause').addClass('icoplay');
                    //document.getElementById('playIndicator').innerHTML = '立刻播放';
                }
            }
            catch (e) {
                // Fail silently but show in F12 developer tools console
                 if(window.console && console.error("Error:" + e));
            }
        }
}

function next(){
	preIndex++;
	if(preIndex == length)preIndex=0;
	oAudio.src = srcs[preIndex];
	$('#bg_img').attr('src',images[preIndex]);
	oAudio.play();
	$('#playIco').removeClass('icoplay').addClass('icopause');
	$('#musicList >li').removeClass('cur');
	$('#musicList >li').eq(preIndex).addClass('cur');
}

function select(o){
	$('#musicList >li').removeClass('cur');
	$(o).addClass('cur');
	//oAudio = document.getElementById('player');
	preIndex=$(o).attr('num')
	oAudio.src = srcs[preIndex];
	$('#bg_img').attr('src',images[preIndex]);
	oAudio.play();
	$('#playIco').removeClass('icoplay').addClass('icopause');
}

function timeDeal(time){
	min = parseInt(time/60);
	sec = parseInt(time - 60*min);
	if(sec<10)sec = '0'+sec;
	return '0'+min+':'+sec
}